using System;
using System.Collections.Generic;

namespace SiliverlightJavascriptIntelli
{
    public static class JavaScriptConvertersGenerator
    {

        public static string GetConverterJavascript(List<SilverlightType> TypesToGenerateJavascriptFor)
        {
            string ReturnValue = string.Empty;

            ReturnValue += JavascriptFormats.ConverterTypeJavascriptClassDeclaration + JavascriptFormats.BreakLine;

            foreach (SilverlightType typeToGetConverterFor in TypesToGenerateJavascriptFor)
            {
                ReturnValue += GetConverterForType(typeToGetConverterFor) + JavascriptFormats.BreakLine;
            }

            ReturnValue = StringHelper.RemoveFunctionTrailingChars(ReturnValue);

            ReturnValue += JavascriptFormats.ConverterTypeJavascriptClassEndDeclaration + JavascriptFormats.BreakLine;


            return ReturnValue;
        }

        private static string GetConverterForType(SilverlightType TypesToGenerateJavascriptFor)
        {
            return StringHelper.Format(JavascriptFormats.ConverterFunctions,
            TypesToGenerateJavascriptFor.Name,
                ',' + String.Join(",", TypesToGenerateJavascriptFor.AllTypeHierchy.ToArray()) + ',',
                StringHelper.ConvertToJSBool(TypesToGenerateJavascriptFor.CanBePrimitiveType));
        }

        private static string GetIsPrimitiveConditionFor(bool canBePrimitiveType)
        {
            if (canBePrimitiveType)
                return JavascriptFormats.IsPrimitive;
            else
                return string.Empty;
        }
    }
}